package com.yykj.hospital.server.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yykj.common.vo.PageData;
import com.yykj.common.vo.ResultVO;
import com.yykj.hospital.server.dto.HospitalDoctorBindLabelListDTO;
import com.yykj.hospital.server.dto.HospitalLabelImportDTO;
import com.yykj.hospital.server.form.HospitalDoctorBindLabelListForm;
import com.yykj.hospital.server.form.HospitalLabelBindForm;
import com.yykj.hospital.server.form.HospitalLabelListForm;
import com.yykj.hospital.server.handler.HospitalLabelHandler;
import com.yykj.hospital.server.model.entity.HospitalLabel;
import com.yykj.hospital.server.service.HospitalLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by chendg
 * on 2021/10/31 0031.
 */
@RequestMapping("/api/backstage/label")
@SuppressWarnings("unchecked")
@Api(tags = "医生标签管理")
@RestController
public class HospitalLabelController {

    @ApiOperation("标签 -- 添加")
    @PostMapping("/add")
    @ApiOperationSupport(ignoreParameters = {"id", "addTime", "modifyTime"})
    public ResultVO<HospitalLabel> addHospitalLabel(@RequestBody HospitalLabel label) {
        return ResultVO.success(hospitalLabelService.editHospitalLabel(label));
    }

    @ApiOperation("标签 -- 修改")
    @PutMapping("/edit")
    @ApiOperationSupport(ignoreParameters = {"addTime", "modifyTime"})
    public ResultVO<HospitalLabel> editHospitalLabel(@RequestBody HospitalLabel label) {
        return ResultVO.success(hospitalLabelService.editHospitalLabel(label));
    }

    @ApiOperation("标签 -- 列表")
    @GetMapping("/list")
    public ResultVO<PageData<HospitalLabel>> listHospitalLabel(@RequestParam Integer hospitalId,
                                                               @RequestParam(required = false) String name,
                                                               @RequestParam(defaultValue = "1") Integer currentPage,
                                                               @RequestParam(defaultValue = "10") Integer pageSize) {
        HospitalLabelListForm form = HospitalLabelListForm.builder()
                .hospitalId(hospitalId)
                .name(name)
                .currentPage(currentPage)
                .pageSize(pageSize)
                .build();

        return ResultVO.success(hospitalLabelService.listHospitalLabel(form));
    }

    @PostMapping("/upload")
    @ApiOperation("标签 -- 导入")
    public ResultVO upload(Integer hospitalId, MultipartFile file) {
        try {
            HospitalLabelHandler handler = new HospitalLabelHandler();
            ImportParams params = new ImportParams();
            params.setVerifyHandler(handler);
            params.setNeedVerify(true);
            ExcelImportResult<HospitalLabelImportDTO> result = ExcelImportUtil.importExcelMore(file.getInputStream(),
                    HospitalLabelImportDTO.class, params);
            if (result.isVerifyFail()) {
                return ResultVO.error(result.getFailList());
            } else {
                boolean b = hospitalLabelService.checkHospitalLabelList(hospitalId, result);
                if (b) {
                    hospitalLabelService.importHospitalLabelList(hospitalId, result.getList());
                    return ResultVO.success(result.getList());
                } else {
                    return ResultVO.error(result.getFailList());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("请按照模板正确填写");
        }
    }


    @ApiOperation("标签绑定 -- 添加")
    @PostMapping("/bind")
    @ApiOperationSupport(ignoreParameters = {"addTime", "modifyTime"})
    public ResultVO bindHospitalLabel(@RequestBody HospitalLabelBindForm form) {
        hospitalLabelService.bindHospitalLabel(form);
        return ResultVO.success();
    }

    @ApiOperation("标签绑定 -- 删除")
    @DeleteMapping("/bind/delete/{id}")
    public ResultVO deleteBindHospitalLabel(@PathVariable Integer id) {
        hospitalLabelService.deleteBindHospitalLabel(id);
        return ResultVO.success();
    }

    @ApiOperation("标签绑定 -- 绑定列表")
    @GetMapping("/bind/list")
    public ResultVO<PageData<HospitalDoctorBindLabelListDTO>> listBindHospitalLabel(
            @RequestParam(name = "hospitalId") Integer hospitalId,
            @RequestParam(name = "name",required = false) String name,
            @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        HospitalDoctorBindLabelListForm form = HospitalDoctorBindLabelListForm.builder()
                .hospitalId(hospitalId)
                .name(name)
                .currentPage(currentPage)
                .pageSize(pageSize)
                .build();
        return ResultVO.success(hospitalLabelService.listBindHospitalLabel(form));
    }

    private HospitalLabelService hospitalLabelService;

    @Autowired
    public void setHospitalLabelService(HospitalLabelService hospitalLabelService) {
        this.hospitalLabelService = hospitalLabelService;
    }
}
